package jianzhiOffer;

import java.util.PriorityQueue;
import java.util.Queue;

public class MedianFinder {

        /** initialize your data structure here. */
        Queue<Integer> A,B;
        public MedianFinder() {
            A = new PriorityQueue<>();
            B = new PriorityQueue<>((x,y) -> (y - x));
        }

        public void addNum(int num) {
            if(A.size() != B.size()) {
                B.add(num);
                A.add(B.poll());
            }else {
                A.add(num);
                B.add(A.poll());
            }
        }

        public double findMedian() {
            return A.size() == B.size() ? (A.peek() + B.peek()) * 1.0 / 2.0 : B.peek();
        }
}

